<template>
  <div class="main_box" style="padding:10px;">
    <div class="add_wrap">
      <div class="tit">
        <h2>{{$route.query.id?"修 改 医 院 子 项 目 信 息":"新 增 医 院 子 项 目"}}</h2>
        <el-button
          type="success"
          plain
          size="small"
          icon="fa fa-mail-forward"
          @click="returnList(0)"
        >
          返 回</el-button
        >
      </div>
      <el-form
        :model="dataForm"
        ref="dataForm"
        label-width="200px"
        :rules="roleRules"
        style="margin: 0;"
      >
        <el-row>
          <el-form-item label="子项目名称" prop="subItemName">
            <el-input
              v-model="dataForm.subItemName"
              clearable
              maxlength="30"
              show-word-limit
            ></el-input>
          </el-form-item>
          <el-form-item label="子项目别称" prop="subItemShortName">
            <el-input
              v-model="dataForm.subItemShortName"
              clearable
              maxlength="30"
              show-word-limit
            ></el-input>
          </el-form-item>
          <HospitalSelect
            @hospitalId="getHospitalId"
            :disabled="modifyDisabled"
            :site="1"
            :id="dataForm.hospitalId"
          ></HospitalSelect>
          <el-form-item label="关联康来(子)项目" prop="reportSubItemId">
            <el-select
              v-model="dataForm.reportSubItemId"
              filterable
              placeholder="请选择康来子项目"
              :loading="loadingIns"
            >
              <el-option
                v-for="item in pickReportSubItemList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="原价格" prop="subItemPrice">
            <el-input v-model="dataForm.subItemPrice" type="number"></el-input>
          </el-form-item>
          <el-form-item label="合作价格" prop="subItemSalePrice">
            <el-input
              v-model="dataForm.subItemSalePrice"
              type="number"
            ></el-input>
          </el-form-item>
          <el-form-item label="临床意义" prop="subItemMeaning" clearable>
            <el-input
              type="textarea"
              v-model="dataForm.subItemMeaning"
              placeholder="请输入临床意义"
            ></el-input>
          </el-form-item>
          <el-form-item label="注意事项" prop="subItemNote" clearable>
            <el-input
              type="textarea"
              v-model="dataForm.subItemNote"
              placeholder="请输入注意事项"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark" clearable>
            <el-input
              type="textarea"
              v-model="dataForm.remark"
            ></el-input>
          </el-form-item>
        </el-row>
        <!-- 表单保存和提交 -->
        <el-row>
          <el-form-item>
            <el-button type="primary" @click="sureDrugClick"
              >保 存</el-button
            >
            <el-button  @click="returnList(0)"
              >取 消</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as message from "@/constants/message";
import HospitalSelect from "@/components/HospitalSelect";
import HospitalApi from "@/api/institution/hospitalItem";
import ReportItemApi from "@/api/report/reportItem";

export default {
  data() {
    return {
      roleRules: {
        subItemName: [
          {
            required: true,
            message: "请输入项目名称",
            trigger: "blur"
          }
        ],
        hospitalId: [
          {
            required: true,
            message: "请选择对应医院",
            trigger: "change"
          }
        ]
      },
      dataForm: {
        // 表单数据
        subItemId: "",
        subItemName: "",
        subItemShortName: "",
        hospitalId: "",
        subItemPrice: "",
        subItemSalePrice: "",
        reportSubItemId: "",
        subItemMeaning: "",
        subItemNote: "",
        remark:"",
      },
      pickReportSubItemList: [],
      loadingIns: false,
      modifyDisabled: false,
    };
  },
  components: {
    HospitalSelect
  },
  mounted() {
    this.init();
    if(this.$route.query.hospitalFindId){
      this.dataForm.hospitalId = this.$route.query.hospitalFindId
    }
  },
  methods: {
    init() {
      this.$Utils.clearData(this.dataForm);
      this.listReportSubItem();
      if (this.$route.query.id) {
        this.dataForm.subItemId = this.$route.query.id;
        this.getData();
      }
    },
    sureDrugClick() {
      // 提交
      let _this = this;
      _this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (this.dataForm.subItemId) {
            //做一个基础的判断，id不为空就编辑，id为空就增加
            this.$confirm(
              message.MSG_CONFIRM_SAVE_ALTER,
              message.TITLE_PROMPT,
              {}
            ).then(() => {
              this.editData();
            });
          } else {
            this.$confirm(
              message.MSG_CONFIRM_SAVE_ADD,
              message.TITLE_PROMPT,
              {}
            ).then(() => {
              this.addData();
            });
          }
        }
      });
    },
    addData() {
      let _this = this; //限制作用域
      let cond = {
        // 表单数据
        subItemName: _this.dataForm.subItemName,
        subItemMeaning: _this.dataForm.subItemMeaning,
        subItemNote: _this.dataForm.subItemNote,
        subItemShortName: _this.dataForm.subItemShortName,
        hospitalId: _this.dataForm.hospitalId,
        reportSubItemId: _this.dataForm.reportSubItemId,
        subItemPrice: _this.dataForm.subItemPrice,
        subItemSalePrice: _this.dataForm.subItemSalePrice,
        remark:_this.dataForm.remark
      };
      HospitalApi.addHospitalSubItem(cond).then(data => {
        this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ACTION_SUCCESS
        });
        this.returnList(cond);
      });
    },
    editData() {
      // 编辑
      let _this = this;
      let cond = {
        // 表单数据
        subItemId: _this.dataForm.subItemId,
        subItemName: _this.dataForm.subItemName,
        subItemMeaning: _this.dataForm.subItemMeaning,
        subItemNote: _this.dataForm.subItemNote,
        subItemShortName: _this.dataForm.subItemShortName,
        hospitalId: _this.dataForm.hospitalId,
        reportSubItemId: _this.dataForm.reportSubItemId,
        subItemPrice: _this.dataForm.subItemPrice,
        subItemSalePrice: _this.dataForm.subItemSalePrice,
        remark:_this.dataForm.remark
      };
      HospitalApi.editHospitalSubItem(cond)
        .then(data => {
          this.$notify.success({
            title: message.TITLE_SUCCESS,
            message: message.MSG_ACTION_SUCCESS
          });
          this.returnList(cond);
        })
        .catch(error => {
          if (error.msg) {
            this.$message.error(message.MSG_ACTION_FAIL);
          }
        });
    },
    getData() {
      // 编辑前，获取数据
      let _this = this;
      _this.modifyDisabled = true;
      HospitalApi.getHospitalSubItem({ itemId: this.$route.query.id }).then(
        res => {
          this.$Utils.bindingBean(this.dataForm, res.data);
          // 重新赋值
          if (res.data.itemPrice) {
            this.dataForm.subItemPrice = parseInt(res.data.subItemPrice);
          }
          if (res.data.itemSalePrice) {
            this.dataForm.subItemSalePrice = parseInt(
              res.data.subItemSalePrice
            );
          }
          if (res.data.reportSubItemId) {
            this.dataForm.reportSubItemId = parseInt(res.data.reportSubItemId);
          }
          // if (res.data.hospitalId){
          //   this.dataForm.hospitalId = parseInt(res.data.hospitalId)
          // }
        }
      );
    },
    listReportSubItem() {
      // 获取报告单子项目列表
      let _this = this;
      _this.loadingIns = true;
      ReportItemApi.listReportSubItems({})
        .then(res => {
          setTimeout(() => {
            _this.loadingIns = false;
            _this.pickReportSubItemList = res.data.map(item => {
              return {
                value: item.subItemId,
                label: item.subItemName
              };
            });
          }, 200);
        })
        .catch(error => {
          _this.pickReportSubItemList = [];
          console.error(error);
        });
    },
    getHospitalId(val) {
      // 将组件中的id 附给搜索的按钮
      this.dataForm.hospitalId = parseInt(val);
    },
    returnList(val) {
      let _this = this;
      if (val) {
        // console.log(val.hospitalId,"这里是带数据的返回")
        _this.$store.dispatch("delView", this.$route);
        _this.$router.push({
          path: "/item/hospitalSubItemManagement",
          query: { id: val.hospitalId }
        });
      } else {
        // console.log("这里是不带参数的返回")
        this.$store.dispatch("delView", this.$route);
        this.$router.push({ path: "/item/hospitalSubItemManagement" });
      }
    }
  }
};
</script>

<style lang="scss" src="@/assets/css/views/add.scss" scope></style>
<style scoped>
.add_wrap {
  padding-top: 10px;
  padding-left: 23px;
}
</style>
